<?php
session_start();
require_once "include/database.php";
$db = db_connect();
require_once "include/auth.php";
$current_user = current_user();
require_login();
require_user(0);

$student_id_query = $db->prepare("SELECT Student_ID FROM registers AS reg
																	JOIN SectionComplete AS sec ON reg.CRN = sec.CRN
																	WHERE Term =  'Fall 2012'	AND reg.Student_ID = :studentID AND sec.Code = :code");
$student_id_query->execute(array("studentID" => $_POST['studentid'], "code" => $_POST['dropdown_code']));



if($student_id_result = $student_id_query->fetch()){
	$crn_query = $db->prepare("SELECT SC.CRN AS CRN FROM SectionComplete AS SC 
													JOIN registers as reg ON SC.CRN = reg.CRN 
													WHERE reg.Student_ID = :studentID AND SC.Code = :code");
	$crn_query->execute(array("studentID" => $_POST['studentid'], 
													"code" => $_POST['dropdown_code']));
	$crn_result = $crn_query->fetch();

	$log_insert_query = $db->prepare("INSERT INTO logs_visit(Tutor_Student_ID, Student_ID, CRN) 
																		VALUES (:tutID, :studentID, :CRN)");
	$log_insert_query->execute(array("tutID" => $current_user['studentid'], 
																	"studentID" => $_POST['studentid'], 
																	"CRN" => $crn_result['CRN']));
	header("Location: logbook.php?success=1");
	}else{
		header("Location: logbook.php?error=1");
  	exit("You are being redirected");
}
?>